from django.shortcuts import render
from django.views import View
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.views import APIView
from book import models
from book import seriaozers
from django.http import HttpResponse
from  rest_framework.generics import GenericAPIView
from rest_framework import mixins, viewsets
from  rest_framework import views
from  rest_framework.viewsets import ModelViewSet
from  rest_framework.decorators import action
from  rest_framework.pagination import PageNumberPagination
from  rest_framework import  filters 


# Create your views here.
# class Booklist(GenericAPIView):
#     queryset = models.Book.objects.all()
#     serializer_class = seriaozers.Bookseriaozers
#     def get(self,request):
#         ser =self.get_serializer(self.get_queryset(),many=True)
#         return Response({"msg":"success","data":ser.data,"code":200})
#     def post(self,request):
#         ser = self.get_serializer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'message':'添加成功','data':ser.data,'code':201})
#         else:
#             return Response({'message':'添加失败','data':ser.errors,'code':401})
# class Booklist1(GenericAPIView):
#     queryset = models.Book.objects.all()
#     serializer_class = seriaozers.Bookseriaozers
#     def get(self,request,pk):
#         ser = self.get_serializer(self.get_object())
#         return Response({"msg":"success","data":ser.data,"code":200})
#     def put(self,requset,pk):
#         ser = self.get_serializer(self.get_object(),data=requset.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'message':'修改成功','data':ser.data,'code':201})
#         else:
#             return Response({'message':'修改失败','data':ser.errors,'code':401})
#     def delete(self,request,pk):
#         self.get_object().delete()
#         return Response({'message':'删除成功','code':400})
#
#
#
#
# class Publist(GenericAPIView):
#     queryset = models.Publish.objects.all()
#     serializer_class = seriaozers.Pubseriaozers
#     def get(self,request):
#         ser = self.get_serializer(self.get_queryset(),many=True)
#         return Response({"msg":"success","data":ser.data,"code":200})
#     def post(self,request):
#         ser = self.get_serializer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'message':'添加成功','data':ser.data,'code':201})
#         else:
#             return Response({'message':'添加失败','data':ser.errors,'code':401})
# class Publist1(GenericAPIView):
#     queryset = models.Publish.objects.all()
#     serializer_class = seriaozers.Pubseriaozers
#     def get(self,request,pk):
#         ser = self.get_serializer(self.get_object())
#         return Response({"msg":"success","data":ser.data,"code":200})
#     def put(self,request,pk):
#         ser = self.get_serializer(self.get_object(),data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'message':'修改成功','data':ser.data,'code':201})
#         else:
#             return Response({'message': '修改失败', 'data': ser.errors, 'code': 401})
#     def delete(self,request,pk):
#         self.get_object().delete()
#         return Response({'message':'删除成功','code':400})


# class Publist(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin):
#     queryset = models.Publish.objects.all()
#     serializer_class=seriaozers.Pubseriaozers
#     def get(self,request):
#         return self.list(request)
#     def post(self,request):
#         return self.create(request)
#
# class Publist1(GenericAPIView,mixins.ListModelMixin,mixins.UpdateModelMixin,mixins.RetrieveModelMixin,mixins.DestroyModelMixin):
#     queryset = models.Publish.objects.all()
#     serializer_class = seriaozers.Pubseriaozers
#
#     # ID查询
#     def get(self, request, pk):
#         return self.retrieve(request, pk)
#
#     # 修改
#     def put(self, request, pk):
#         return self.update(request, pk)
#
#     # 删除
#     def delete(self, request, pk):
#         return self.destroy(request, pk)
#
#
# class Booklist(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin):
#     queryset = models.Book.objects.all()
#     serializer_class=seriaozers.Bookseriaozers
#     def get(self,request):
#         return self.list(request)
#     def post(self,request):
#         return self.create(request)
#
# class Booklist1(GenericAPIView,mixins.ListModelMixin,mixins.UpdateModelMixin,mixins.RetrieveModelMixin,mixins.DestroyModelMixin):
#     queryset = models.Book.objects.all()
#     serializer_class = seriaozers.Bookseriaozers
#
#     # ID查询
#     def get(self, request, pk):
#         return self.retrieve(request, pk)
#
#     # 修改
#     def put(self, request, pk):
#         return self.update(request, pk)
#
#     # 删除
#     def delete(self, request, pk):
#         return self.destroy(request, pk)


# class Booklist(viewsets.ViewSet):
#     def list(self,request):
#         booklist = models.Book.objects.all()
#         ser = seriaozers.Bookseriaozers(booklist,mant=True)
#         return Response({'msg':'ok','data':ser.data})
#     def create(self,request):
#         return Response('添加数据')
#
# class Booklist1(viewsets.ViewSet):
#     def retrieve(self,request,pk):
#         return Response('单选一条数据')
#     def update(self,request,pk):
#         return  Response('修改数据')
#     def destoy(self,request,pk):
#         return  Response('删除数据')

class BookPage(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'




class Booklist(viewsets.ModelViewSet):
    queryset = models.Book.objects.all()
    serializer_class = seriaozers.Bookseriaozers
    pagination_class = BookPage



    # def list(self, request):
    #     return Response({'msg':'ok','data':super().list(request).data})
# class Booklist1(viewsets.ModelViewSet):
#     queryset = models.Book.objects.all()
#     serializer_class = seriaozers.Bookseriaozers
#     def destroy(self, request,pk):
#         super().destroy(request,pk)
#         return Response('ok')

class Publist(viewsets.ModelViewSet):
    queryset = models.Publish.objects.all()
    serializer_class = seriaozers.Pubseriaozers

    @action(detail=False,methods = ['GET'])
    def test(self,request):
        return Response({'msg':'测试'})

    @action(detail=True, methods=['GET'])
    def change(self,requset,pk):
        return Response({'msg':'修改状态'})


# class Publist1(viewsets.ModelViewSet):
#     queryset = models.Publish.objects.all()
#     serializer_class = seriaozers.Pubseriaozers
#     def destroy(self, request,pk):
#         super().destroy(request,pk)
#         return Response('ok')




